<template>
  <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="120">

    <Form-item label="商户名称" prop="name">
      <Input v-model="formValidate.name" placeholder="请输入商户名称"></Input>
    </Form-item>

    <Form-item label="商户联系人" prop="contacts">
      <Input v-model="formValidate.contacts" placeholder="请输入商户联系人名称"></Input>
    </Form-item>

    <Form-item label="联系人手机" prop="phone">
      <Input v-model="formValidate.phone" placeholder="联系人手机"></Input>
    </Form-item>

    <Form-item label="城市" prop="city" placeholder="请选择所在城市">
      <cascader
        size="large"
        :data="options"
        v-model="formValidate.city"
        @on-change="handleChange"
       >
      </cascader>
    </Form-item>

    <Form-item label="详细地址" prop="desc">
      <Input v-model="formValidate.desc" placeholder="详细地址" ></Input>
    </Form-item>

    <Form-item label="地图">
      <baidu-map class="map-container">
        <bm-view class="bm-view">
        </bm-view>
        <bm-local-search :keyword="formValidate.desc" :auto-viewport="true" :resultPanel="false" :location="location"></bm-local-search>
      </baidu-map>
    </Form-item>

    <Form-item>
      <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
      <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
    </Form-item>
  </Form>
</template>

<script>
  import { regionData } from 'element-china-area-data'
  export default {
    data () {
      const validatePhone = (rule, value, callback) => {
        if (!(/^1[34578]\d{9}$/.test(value))) {
          callback(new Error('手机号码格式不正确'))
        } else {
          callback()
        }
      }
      return {
        location: '中国',
        keyword: '',
        options: regionData,
        selectedOptions: [],
        formValidate: {
          city: [],
          name: '',
          contacts: '',
          tel: '',
          gender: '',
          interest: [],
          date: '',
          time: '',
          desc: ''
        },
        ruleValidate: {
          name: [
            { required: true, message: '商户名称不能为空', trigger: 'blur' }
          ],
          contacts: [
            { required: true, message: '商户联系人名称不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系人手机不能为空', trigger: 'blur' },
            { validator: validatePhone, trigger: 'blur' }
          ],
          city: [
            { required: true, type: 'array', message: '请选择城市', trigger: 'change' }
          ],
          gender: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          interest: [
            { required: true, type: 'array', min: 1, message: '至少选择一个爱好', trigger: 'change' },
            { type: 'array', max: 2, message: '最多选择两个爱好', trigger: 'change' }
          ],
          date: [
            { required: true, type: 'date', message: '请选择日期', trigger: 'change' }
          ],
          time: [
            { required: true, type: 'date', message: '请选择时间', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '详细地址不能位空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleChange (value) {
        console.log(value)
      },
      handleSubmit (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.$Message.success('提交成功!')
          } else {
            this.$Message.error('表单验证失败!')
          }
        })
      },
      handleReset (name) {
        this.$refs[name].resetFields()
      }
    }
  }
</script>
<style scoped>
   .map-container .bm-view {
      width: 100%;
      height: 400px
    }
</style>
